package pers.sports.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import pers.sports.service.ApplyService;
import pers.sports.service.ScoreService;
import pers.sports.vo.StuScore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @datetime 2021/4/28 18:04
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    private final ScoreService scoreService;
    private final ApplyService applyService;

    public ScoreController(ScoreService scoreService, ApplyService applyService) {
        this.scoreService = scoreService;
        this.applyService = applyService;
    }

    @RequestMapping("/get")
    public Map getAll(String projectId){
        if ("1".equals(projectId)){
            projectId += " or p.projectId=2";
        }else if("3".equals(projectId)){
            projectId+=" or p.projectId=4";
        }
        List<StuScore> list = scoreService.findScoreByProjectId(projectId);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("automaticImport")
    public String automaticImport(){
        List<String> list = applyService.getReviewPass();
        scoreService.deleteAll();
        for (int i = 0; i < list.size(); i++) {
            scoreService.insertApplyId(list.get(i));
        }
        return "success";
    }

    @RequestMapping(value = "modifyGrade",method = RequestMethod.POST)
    public String modifyGrade(@RequestParam("value") String value,@RequestParam("field") String field,@RequestParam("key") String key){
        String sb = (field + "字段已更改为："+value);
        scoreService.updateScoreById(field,key,value);
        return sb;
    }
}
